package com.dchh.android.suverynew.adapter;

import android.app.Activity;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dchh.android.suverynew.R;
import com.dchh.android.suverynew.act.MyDemandActivity;
import com.dhcc.android.base.common.bean.BasicProperties;
import com.dhcc.android.base.utils.ClearEditText;
import com.dhcc.android.base.utils.StringUtil;

import java.util.List;
import java.util.Map;

/**
 * @author zhoudan
 * @date 2016年1月5日
 */
public class AddListAdapter extends BaseAdapter {

    private Activity activity;

    private List<Map<String, String>> datas;

    private List<BasicProperties> fieldList;

    private OnItemDeleteListener onDelete;

    private String key;

    public AddListAdapter(Activity activity, List<Map<String, String>> datas, List<BasicProperties> fieldList, String key) {
        this.activity = activity;
        this.datas = datas;
        this.fieldList = fieldList;
        this.key = key;
    }

    public void refresh(List<Map<String, String>> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    public void setOnDeleteListener(OnItemDeleteListener onDelete) {
        this.onDelete = onDelete;
    }


    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Map<String, String> getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View row = LayoutInflater.from(activity).inflate(R.layout.util_add_list_item, null);
        ImageView delete = (ImageView) row.findViewById(R.id.delete);
        TextView value = (TextView) row.findViewById(R.id.value);
        LinearLayout subcontent = (LinearLayout) row.findViewById(R.id.sub_content);

        Map<String, String> map = getItem(position);
        for (BasicProperties fd : fieldList) {
            if (fd.isShow()) {//显示
                //判读是否是枚举的属性
                String input = fd.getInput();
                if (input.startsWith("5") || input.contains("|5")) {
                    if (fd.getName().equals(key)) {
                        value.setText(map.get(key));
                    }

                } else {//不是枚举类型的属性
                    //渲染
                    LinearLayout pv = (LinearLayout) LayoutInflater.from(activity).inflate(R.layout.util_add_list_item_prop, null);
                    TextView label = (TextView) pv.findViewById(R.id.prop_label);
                    TextView normal = (TextView) pv.findViewById(R.id.normal_prop);
                    ClearEditText edit = (ClearEditText) pv.findViewById(R.id.edit_prop);
                    Button btn = (Button) pv.findViewById(R.id.prop_btn);

                    //设值
                    label.setText(fd.getLabel());
                    String val = StringUtil.emptyValueFormatter(map.get(fd.getName()));
                    if (fd.isEditable()) {
                        edit.setText(val);
                        edit.setVisibility(View.VISIBLE);
                        normal.setVisibility(View.GONE);
                        if (input.contains("001")) {
                            edit.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                        } else {
                            edit.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL);
                        }
                    } else {
                        normal.setText(val);
                        edit.setVisibility(View.GONE);
                        normal.setVisibility(View.VISIBLE);
                    }

                    edit.addTextChangedListener(new TextChange(edit, fd.getName(), position));
                    normal.addTextChangedListener(new TextChange(normal, fd.getName(), position));

                    btn.setText(fd.getButtonlabel());
                    if (fd.getButtonlabel() != null) {
                        btn.setVisibility(View.VISIBLE);
                    } else {
                        btn.setVisibility(View.GONE);
                    }

                    label.setVisibility(View.VISIBLE);
                    subcontent.addView(pv);
                }
            }
        }


        delete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (onDelete != null) {
                    onDelete.OnDelete(position);
                }
            }
        });

        return row;
    }

    public class TextChange implements TextWatcher {
        private TextView txv;
        private String key;
        private int position;

        public TextChange(TextView txv, String key, int position) {
            this.txv = txv;
            this.key = key;
            this.position = position;
        }

        @Override
        public void afterTextChanged(Editable s) {
            String value = txv.getText().toString().trim();
            Map<String, String> map = datas.get(position);
            map.put(key, value);
            datas.remove(position);
            datas.add(position, map);

            ((MyDemandActivity) activity).selectSave = true;
            ((MyDemandActivity) activity).refreshTBMJ("ZWMJ");
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

    }

    public interface OnItemDeleteListener {
        public void OnDelete(int position);
    }
}
